<?php 

namespace Modules\Ddtalk\Lib;
  
  //接口需参看dingding文档:
  //http://g.alicdn.com/dingding/opendoc/docs/_server/tab2.html?t=1466131884821


  // usage:
  // 
  //  Departmant::deptList()
  //  Departmant::userList(123321,20,0)  //简单用户信息,  第一页 20个
  //  Departmant::userListEx(123321,20,0)  //复杂用户信息
  //  Departmant::getDept(123321) //获取部门详细信息
  //  Departmant::createDept([name=>name,parentid=>parentid,order=>order,
  //          createDeptGroup=>createDeptGroup]) //创建部门.  
  //  Departmant::updateDept(data) //更新部门, 参数见钉钉文档
  //  Departmant::deleteDept(id) //删除部门

   //
   //  $dept= Departmant::getDept(123321)
   //  $dept->name = "新部门名字";
   //  $dept->update(); //更新部门名字为 "新部门名字"
   //  $dept->delete(); //删除该部门.

class Department {

	const API_DEPARTMENT_LIST = "/department/list";
	const API_DEPARTMENT_GET = "/department/get";
	const API_DEPARTMENT_CREATE= "/department/create";
	const API_DEPARTMENT_UPDATE = "/department/update";
	const API_DEPARTMENT_DELETE = "/department/delete";
	const API_DEPARTMENT_USER_LIST = "/user/simplelist";
	const API_DEPARTMENT_USER_DETAIL_LIST = "/user/list";

	


	private $id;
	private $parentid=null; //防止不必要的错误, i与dingding协议接口相同,也使用小写的.
	private $name;
	private $order;
	private $createDeptGroup;
	private $autoAddUser;
	private $deptHiding;
	private $deptPerimits;
	private $userPerimits;
	private $outerDept;
	private $outerPermitDepts;
	private $outerPermitUsers;
	private $orgDeptOwner;
	private $deptManagerUseridList;
	private $lang;

	private $__dirty = [];

	public function &__get($name) {
		return $this->$name;
	}

	public function __set($name, $value) {
		if ( $value === $this->$name || $name == 'id' || $name=='lang' ) {
			return;
		}
		if ( $this->$name !== $value ) {
			$this->__dirty[$name] = 1;
		} 
		$this->$name = $value;
	}

	public static function deptList($lang='zh_CN') {//en_US
		return Util::request(self::API_DEPARTMENT_LIST,['lang'=>$lang], true, 'get');
	}
	
	public static function userList($id,$size=0,$page=0,$order='custom',$lang='zh_CN') {
		$params = [];
		$params['department_id'] = $id;
		if ( $size != 0 && $page != 0 ) {
			$params['size'] = $size;
			$params['page'] = $page;
		}
		$params['order'] = $order;
		$params['lang'] = $lang;
		return Util::request(self::API_DEPARTMENT_USER_LIST,$params, true, 'get');
	}

	public static function userListEx($id,$size=0,$page=0,$order='custom',$lang='zh_CN') {
		$params = [];
		$params['department_id'] = $id;
		if ( $size != 0 && $page != 0 ) {
			$params['size'] = $size;
			$params['page'] = $page;
		}
		$params['order'] = $order;
		$params['lang'] = $lang;
		return Util::request(self::API_DEPARTMENT_USER_DETAIL_LIST,$params, true, 'get');
	}


	public static function getDept($id,$lang='zh_CN') {
		$data = Util::request(self::API_DEPARTMENT_USER_DETAIL_LIST,['lang'=>$lang,'id'=>$id], true, 'get');
		if ($data['errcode'] == 0 ) {
			return new Department($data,$lang);
		} else {
			return null;
		}
	}
	
	public static function createDept($name,$parentid,$order=null,$createDeptGroup=null) {
		$params = [];
		$params['name'] = $name;
		$params['parentid'] = $parentid;
		if ( $order !== null) {
			$params['order'] = $order;
		}
		if ( $createDeptGroup !== null) {
			$params['createDeptGroup'] = $createDeptGroup;
		}
		return Util::request(self::API_DEPARTMENT_CREATE,$params, true);
	}

	public static function updateDept($data) {
		if(isset($data['deptManagerUseridList']) && is_array($data['deptManagerUseridList']) ) {
			$data['deptManagerUseridList'] = implode('|',$data['deptManagerUseridList']);
		}
		if(isset($data['deptPerimits']) && is_array($data['deptPerimits']) ) {
			$data['deptPerimits'] = implode('|',$data['deptPerimits']);
		}
		if(isset($data['userPerimits']) && is_array($data['userPerimits']) ) {
			$data['userPerimits'] = implode('|',$data['userPerimits']);
		}
		if(isset($data['outerPermitDepts']) && is_array($data['outerPermitDepts']) ) {
			$data['outerPermitDepts'] = implode('|',$data['outerPermitDepts']);
		}
		if(isset($data['outerPermitUsers']) && is_array($data['outerPermitUsers']) ) {
			$data['outerPermitUsers'] = implode('|',$data['outerPermitUsers']);
		}
		return Util::request(self::API_DEPARTMENT_UPDATE,$data, true);
	}

	public static function deleteDept($id) {

		return Util::request(self::API_DEPARTMENT_DELETE,['id'=>$id], true, 'get');
	}
	
	public function delete() {
		return Department::deleteDept($this->id);
	}
	
	public function update() {
		$data = [];
		foreach ($this->__dirty as $key => $value ) {
			$data[$key] = $this->$key;
		}
		if (empty($data)) {
			return false;
		} else {
			$data['id'] = $this->id;
			return Department::updateDept($data);
		}
	}

	protected function __construct($data,$lang='zh_CN') {
		$this->lang = $lang;
		$this->autoAddUser = $data['autoAddUser'];
		$this->createDeptGroup = $data['createDeptGroup'];
		$this->deptHiding = $data['deptHiding'];
		$this->deptManagerUseridList = array_map('trim',explode('|',$data['deptManagerUseridList']));
		$this->deptPerimits = array_map('trim',explode('|',$data['deptPerimits']));
		$this->id = $data['id'];
		if (isset($data['parentid'])) {
			$this->parentid = $data['parentid'];	
		}
		$this->name = $data['name'];
		$this->order = $data['order'];
		$this->orgDeptOwner = $data['orgDeptOwner'];
		$this->outerDept = $data['outerDept'];
		if ( empty(trim($data['outerPermitDepts'])) ) {
			$this->outerPermitDepts = [];
		}
		$this->outerPermitDepts = array_map('trim',explode('|',$data['outerPermitDepts']));
		$this->outerPermitUsers = array_map('trim',explode('|',$data['outerPermitUsers']));
		$this->userPerimits = array_map('trim',explode('|',$data['userPerimits']));
	}
}





